package com.he.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

public class ImgUtil {
	//STR中避免出现字符和数组长得像的情况
	public static String STR="abcdefghijkmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXY3456789";
	public static int LEN=4;//验证码长度
	public static int WIDTH=15;//字符宽度
	public static int HEIGHT=30;//字符高度
	public static int SPAN=4;//字符间距
	public static int IMGWIDTH=(WIDTH+SPAN)*LEN;//图片宽度=（字符宽度+字符间距）*4
	
	//从STR中随机获取LEN长度的char数组
	public static char[] getStr() {
		char[] result=new char[LEN];//将验证码存在char数组
		for (int i = 0; i < LEN; i++) {
			Random random=new Random();
			result[i]=STR.charAt(random.nextInt(STR.length()));
		}
		return result;
	}
	public static BufferedImage getImage(char[] array) {
		//创建画纸
		BufferedImage img=new BufferedImage(IMGWIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		//创建画笔
		Graphics graphics=img.getGraphics();
		//设置背景颜色
		graphics.setColor(Color.BLACK);
		//设置背景色
		graphics.fillRect(0, 0, IMGWIDTH, HEIGHT);
		// 设置字体
		Font font=new Font("微软雅黑",Font.BOLD , 16);
		graphics.setFont(font);
		// 设置画笔颜色
		graphics.setColor(Color.WHITE);
		for (int i = 0; i < array.length; i++) {
			graphics.drawString(String.valueOf(array[i]), (WIDTH+SPAN)*i, HEIGHT-10);
		}
		return img;
	}
	
}
